package com.mtoolkit.jdbc.handler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * Object array result set call back handler.
 * 
 * @author 	<a href="mailto:xishizhang@gmail.com">ZhangShixi</a>
 * @version 1.0, 12/7/2011
 * @since 	JDK1.5
 */
public class ArrayResultSetHandler implements ResultSetHandler<Object[]> {

	/**
     * Handles the result set and return object array result.
     * 
     * @param  rs result set.
     * 
     * @return object array result.
     * 
     * @throws SQLException if a sql error occurs whiling handle result set.
     */ 
    @Override
    public Object[] handle(ResultSet rs) throws SQLException {
        if (!rs.next()) {
            return null;
        }

        ResultSetMetaData meta = rs.getMetaData();
        int columnCount = meta.getColumnCount();
        Object[] results = new Object[columnCount];

        for (int i = 0; i < columnCount; i++) {
            results[i] = rs.getObject(i + 1);
        }

        return results;
    }
    
}
